/**
 * 全局变量
 */
var pageNoOne;//页码
var pageSizeOne;//页码大小
var countOne;//总数
var supplierName = "";//供应商名称

function ejectSupplier(){
	$.jBox($("#divSupplier").html(), {
		id: 'jBoxSupplier', 
		title : "选择供应商",
		width : 'auto',
		height : 'auto',
		buttons : {"选择" : false , "关闭" : true },
		submit : function(v, h, f) {
			if (v == false) {
				if(h.find("input[name='radio']:checked").val() != null){
    				$("#supplier").attr("value",h.find("input[name='radio']:checked").parent().parent().children("td")[2].innerHTML);
    				$("#supplierCodeNum").attr("value",h.find("input[name='radio']:checked").parent().parent().children("td")[1].innerHTML);
    				$.jBox.close(jBoxSupplier);
    				addPro(); 
    				return true;
				}else{
					$.jBox.tip("请选择供应商！", "warn");
					return false;
				}
			}
		}
	});
}

/**
 * 供应商信息
 */
$("#supplier").live('dblclick',function(){
	$("#rowSupplier").html("");
	var newRows = '';
	 $.getJSON("/jeesite/a/pro/realDetail/supplierList",function(data){
		 if (typeof(data.list) != "undefined" && data.list.length >= 1){
			$.each(data.list, function(i,supplier){
				newRows += '<tr><td style="width: 8px;">'
					 +'<input type="radio"class="subBox" name="radio" value=""/></td>'
					 +'<td>'+supplier.codeNum+'</td>'
					 +'<td>'+supplier.name+'</td>'
					 +'<td>'+supplier.phone+'</td>'
					 +'<td>'+supplier.fax+'</td>'
					 +'<td>'+supplier.email+'</td>'
					 +'<td>'+supplier.website+'</td>'
					 +'<td>'+supplier.address+'</td>'
					 +'<td style="display:none;">'+supplier.id+'</td></tr>';
			});
		 }
		$("#rowSupplier").append(newRows);
		pageNoOne = data.pageNo;
		pageSizeOne = data.pageSize;
		countOne = data.count;
		$("#supplierPageNo").html(pageNoOne);
		$("#supplierPageSize").html(pageSizeOne);
		$("#supplierCount").html(countOne);
		if(pageNoOne <= 1){
			$("#supplierPreviousPageLi").attr("class","disabled");
		}
		if(pageNoOne >= countOne/pageSizeOne){
			$("#supplierNextPageLi").attr("class","disabled");
		}
		//弹出选择框
		ejectSupplier();
	});
});

/**
 * 搜索供应商
 * @param obj
 */
function selectSupplier(obj){
	supplierName = $(obj).prev().val();
	$("#rowSupplier").html(" ");
	var  newRow = "";
	$.getJSON("/jeesite/a/pro/realDetail/supplierList","name="+supplierName,function(data){
		if (typeof(data.list) != "undefined" && data.list.length >= 1){
			$.each(data.list, function(i,supplier){
				newRow += '<tr><td style="width: 8px;">'
					 +'<input type="radio"class="subBox" name="radio" value=""/></td>'
					 +'<td>'+supplier.codeNum+'</td>'
					 +'<td>'+supplier.name+'</td>'
					 +'<td>'+supplier.phone+'</td>'
					 +'<td>'+supplier.fax+'</td>'
					 +'<td>'+supplier.email+'</td>'
					 +'<td>'+supplier.website+'</td>'
					 +'<td>'+supplier.address+'</td>'
					 +'<td style="display:none;">'+supplier.id+'</td></tr>';
			});
		}
		$("#rowSupplier").append(newRow);
		pageNoOne = data.pageNo;
		pageSizeOne = data.pageSize;
		countOne = data.count;
		$("#supplierPageNo").html(pageNoOne);
		$("#supplierPageSize").html(pageSizeOne);
		$("#supplierCount").html(countOne);
		if(pageNoOne <= 1){
			$("#supplierPreviousPageLi").attr("class","disabled");
		}
		if(pageNoOne >= countOne/pageSizeOne){
			$("#supplierNextPageLi").attr("class","disabled");
		}
		$.jBox.close(jBoxSupplier);
		ejectSupplier();
	});
}

$("#supplierNextPage").live("click",function(){
	if(pageNoOne < countOne/pageSizeOne){
		$("#rowSupplier").html("");
		var newRow = "";
		$.getJSON("/jeesite/a/pro/realDetail/supplierList","pageNo="+(pageNoOne+1)+"&name="+supplierName,function(data){
			if (typeof(data.list) != "undefined" && data.list.length >= 1){	
				$.each(data.list, function(i,supplier){
					 newRow += '<tr><td style="width: 8px;">'
						 +'<input type="radio"class="subBox" name="radio" value=""/></td>'
						 +'<td>'+supplier.codeNum+'</td>'
						 +'<td>'+supplier.name+'</td>'
						 +'<td>'+supplier.phone+'</td>'
						 +'<td>'+supplier.fax+'</td>'
						 +'<td>'+supplier.email+'</td>'
						 +'<td>'+supplier.website+'</td>'
						 +'<td>'+supplier.address+'</td>'
						 +'<td style="display:none;">'+supplier.id+'</td></tr>';
				});
			}
			 $("#rowSupplier").append(newRow);
			pageNoOne = data.pageNo;
			pageSizeOne = data.pageSize;
			countOne = data.count;
			$("#supplierPageNo").html(pageNoOne);
			$("#supplierPageSize").html(pageSizeOne);
			$("#supplierCount").html(countOne);
			$("#supplierPreviousPageLi").attr("class","");
			if(pageNoOne <= 1){
				$("#supplierPreviousPageLi").attr("class","disabled");
			}
			if(pageNoOne >= countOne/pageSizeOne){
				$("#supplierNextPageLi").attr("class","disabled");
			}
			$.jBox.close(jBoxSupplier);
			ejectSupplier();
		});
	}
});

/**
* 客户上一页
*/
$("#supplierPreviousPage").live("click",function(){
	if(pageNoOne > 1){
		$("#rowSupplier").html("");
		var newRow = "";
		$.getJSON("/jeesite/a/pro/realDetail/supplierList","pageNo="+(pageNoOne-1)+"&name="+supplierName,function(data){
			if (typeof(data.list) != "undefined" && data.list.length >= 1){
				$.each(data.list, function(i,supplier){
					 newRow += '<tr><td style="width: 8px;">'
						 +'<input type="radio"class="subBox" name="radio" value=""/></td>'
						 +'<td>'+supplier.name+'</td>'
						 +'<td>'+supplier.phone+'</td>'
						 +'<td>'+supplier.fax+'</td>'
						 +'<td>'+supplier.email+'</td>'
						 +'<td>'+supplier.website+'</td>'
						 +'<td>'+supplier.address+'</td>'
						 +'<td style="display:none;">'+supplier.id+'</td></tr>';
				});
			}
			$("#rowSupplier").append(newRow);
			pageNoOne = data.pageNo;
			pageSizeOne = data.pageSize;
			countOne = data.count;
			$("#supplierPageNo").html(pageNoOne);
			$("#supplierPageSize").html(pageSizeOne);
			$("#supplierCount").html(countOne);
			$("#supplierNextPageLi").attr("class","");
			if(pageNoOne <= 1){
				$("#supplierPreviousPageLi").attr("class","disabled");
			}
			if(pageNoOne >= countOne/pageSizeOne){
				$("#supplierNextPageLi").attr("class","disabled");
			}
			$.jBox.close(jBoxSupplier);
			ejectSupplier();
		});
	}
});